'use strict';

angular
  .module('cmsysApp')
  .service('contractService',
    [
      '$http',
      'baseService',
      'pageService',
      function ($http, baseService, pageService) {
        var self = this;
        this.findAllTypes = function ($scope) {
          baseService.query($scope.operations.findAllTypes).then(function (data) {
            $scope.products = data.products;
            $scope.provinces = data.provinces;
            $scope.purposes = data.purposes;
            $scope.categories = data.categories;
          });
        };
        this.contractList = function ($scope) {
          self.params = $scope.params || {};
          pageService.query($scope.params).then(function (page) {
            pageService.loadData($scope, page);
          });
        };
        this.del = function (del) {
          baseService.del(del)
        };
        this.edit = function (edit) {
          baseService.edit(edit)
        };
      }])
  .controller(
    'contractCtrl',
    [
      '$scope',
      '$sce',
      '$http',
      '$state',
      '$stateParams',
      'contractService',
      'pageService',
      '$filter',
      'userPrivileges',
      function ($scope, $sce, $http, $state, $stateParams, contractService, pageService, $filter, userPrivileges) {
        $scope.opt = userPrivileges['删除合同'] || userPrivileges['修改合同'];
        $scope.params = {
          'url': baseUrl + '/contract/contractList',
          'conditions': {
            'condition': '',
            'currentPage': 1,
            'pageSize': 10
          }
        };
        $scope.go = function (pageNum) {
          $scope.params.conditions.currentPage = pageNum;
          contractService.contractList($scope);
        }
        $scope.operations = {
          'del': {
            'url': baseUrl + '/contract/delContract',
            'params': $scope.contract
          },
          'edit': {
            'url': baseUrl + '/contract/editContract',
            'params': $scope.contract
          },
          'findAllTypes': {
            'url': baseUrl + '/contract/findAllTypes'
          }
        }
        $('#textarea').wysihtml5();
        $scope.changePS = function () {
          pageService.changePS($scope);
          contractService.contractList($scope);
        };
        $scope.changeCD = function () {
          pageService.changeCD($scope);
          contractService.contractList($scope);
        };
        $scope.contracts = [{
          'cid': '00001',
          'pname': '建筑',
          'cname': '湖北师范大学',
          'money': '1000万',
          'signperson': '张磊',
          'signdate': '2016-04-12',
          'province': '湖北',
          'materiel': '建筑',
          'purpose': '建楼',
          'category': '教育',
          'scale': '1000人',
          'remark': '这是一个悲伤的故事！'
        }];
        contractService.findAllTypes($scope);
        contractService.contractList($scope);

        $scope.edit = function (index, e) {
          if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
          } else {
            //否则我们使用ie的方法来取消事件冒泡
            window.event.cancelBubble = true;
          }
          $scope.title = '修改合同';
          var contract = $scope.page.recordList[index];
          contract.signdate1 = new Date(contract.signdate);
          contract.purpose = contract.purpose + "";
          contract.pid = contract.pid + "";
          contract.category = contract.category + "";
          $scope.contract = contract;
          // $('#textarea').data("wysihtml5").editor.setValue(contract.remark);
          $('#contract').modal('toggle');
        };
        $scope.del = function (index, e) {
          if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
          } else {
            //否则我们使用ie的方法来取消事件冒泡
            window.event.cancelBubble = true;
          }
          $scope.operations.del.params = {cid: index};
          contractService.del($scope.operations.del);
          contractService.contractList($scope);
        }
        $scope.addContract = function () {
          $scope.title = '添加合同';
          $('#contract').modal('toggle');
        };

        $scope.save = function () {
          $('#contract').modal('toggle');
          $scope.contract['remark'] = $(".textarea").val();
          $scope.contract.signdate = $filter('date')($scope.contract.signdate1, "yyyy/MM/dd");
          $scope.operations.edit.params = $scope.contract;
          contractService.edit($scope.operations.edit);
          contractService.contractList($scope);
          $scope.contract = {};
          $scope.myForm.$setPristine();
        }

        $scope.contractInfo = function (index) {
          $scope.cttInfo = $scope.page.recordList[index];
          $scope.cttInfo.remark = $sce.trustAsHtml($scope.cttInfo.remark + "");
          $('#contractInfo').modal('toggle');
        }

        $scope.cancel = function () {
          $scope.contract = {};
          $scope.myForm.$setPristine();
          $('#contract').modal('toggle');
        }
      }
    ])
;
